package client;

import helpers.Card;
import helpers.PlayerHintData;
import helpers.SelfRemoting;
import helpers.action.Action;
import helpers.action.RaiseAction;
import helpers.enums.CzyStosujeContinuation;
import helpers.enums.IleRakRozgrywa;
import helpers.enums.Suggestion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.List;

import remote.PokerPlayer;

public class DefaultPokerPlayer extends SelfRemoting<PokerPlayer> implements
		PokerPlayer {

	private BufferedReader is;

	public DefaultPokerPlayer() throws RemoteException {
		super();
		is = new BufferedReader(new InputStreamReader(System.in));
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				try {
					is.close();
				} catch (IOException e) {
					// ignore
				}
			}
		});
	}

	private static final long serialVersionUID = -7577508331516240978L;

	@Override
	public void giveHand(Card card1, Card card2) throws RemoteException {
		StringBuilder builder = new StringBuilder();
		builder.append("Received cards:").append("\n");
		builder.append("\t" + card1.toString() + ", " + card2.toString())
				.append("\n");
		System.out.println(builder.toString());
	}

	@Override
	public void inform(String string) throws RemoteException {
		System.out.println(string);
	}

	@Override
	public void informAboutOpponentMove(String string) throws RemoteException {
		System.err.println(string);
	}

	@Override
	public Action chooseAction(List<Action> possibleActions)
			throws RemoteException {
		StringBuilder builder = new StringBuilder();
		builder.append("Possible actions: ");
		for (int i = 0; i < possibleActions.size(); i++) {
			builder.append("[" + i + "]").append(
					possibleActions.get(i).toString()).append(", ");
		}
		System.out.println(builder.substring(0, builder.length() - 2));
		int choosenAction = -1;
		while (choosenAction < 0 || choosenAction > possibleActions.size() - 1) {
			System.out.print("Choose action: ");
			choosenAction = readInt();
		}
		if (possibleActions.get(choosenAction) instanceof RaiseAction) {
			RaiseAction ra = (RaiseAction) possibleActions.get(choosenAction);
			int bettedMoney = -1;
			while (bettedMoney < ra.getMinRaise()
					|| bettedMoney > ra.getMaxRaise()) {
				System.out.print("Amount of money to bet: ");
				bettedMoney = readInt();
			}
			ra.setDecison(bettedMoney);
		}
		return possibleActions.get(choosenAction);
	}

	private int readInt() {
		int result = -1;
		try {
			String line = is.readLine();
			result = Integer.parseInt(line);
		} catch (Exception e) {
			// ignore
		}
		return result;
	}

	@Override
	public PlayerHintData askAboutHint() throws RemoteException {

		System.out.println("Would you like to hear a hint? [0] Yes, [1] No");
		int answer = -1;
		while (answer < 0 || answer > 1) {
			System.out.print("Your answer? ");
			answer = readInt();
		}
		if (answer == 1) {
			System.out.println("\n");
			return null;
		}

		PlayerHintData playerHintData = new PlayerHintData();
		answer = -1;
		System.out
				.println("Does your opponent engage in more than half of total games? [0] Yes [1] No ");
		while (answer < 0 || answer > 1) {
			System.out.print("Your answer? ");
			answer = readInt();
		}
		playerHintData.setIleRakRozgrywa(IleRakRozgrywa.values()[answer]);

		answer = -1;
		System.out
				.println("How often does your opponent use Continuation Bet in game? [0] Always, [1] Often, [2] Seldom, [3] Never");
		while (answer < 0 || answer > 3) {
			System.out.print("Your answer? ");
			answer = readInt();
		}
		playerHintData.setCzyStosujeContinuation(CzyStosujeContinuation
				.values()[answer]);

		answer = -1;
		System.out
				.println("Do you consider your opponent to be a good player? [0] Yes, [1] No");
		while (answer < 0 || answer > 1) {
			System.out.print("Your answer? ");
			answer = readInt();
		}
		playerHintData.setCzyPrzeciwnikJestDobry(answer == 0);

		System.out.println("\n");

		return playerHintData;
	}

	@Override
	public void presentSuggestiion(Suggestion suggestion) {
		System.out.println(suggestion);
	}
}
